from scrapy_redis.spiders import RedisCrawlSpider
from redisoper import CRedis

from items import FruitItem


class FruitSpider(RedisCrawlSpider):
    redis_key = 'fruit_spider:start_urls';
    name = "fruits"


    def __init__(self):
        self.r = CRedis();
        self.r.remove("seq")
        self.initstarturl();

    def parse(self, response):
        sites = response.xpath("//*[@id='sort_list_show']/li");
        items = []

        for site in sites:
            item = FruitItem()
            item['name'] = site.xpath(
                './a/@title').extract_first().strip();

            item['kal'] = site.xpath(
                './p/text()').extract_first().strip()
            items.append(item)
        return items
    def initstarturl(self):
        for i in range(3):
            url="http://fitness.39.net/food/list-6-"+str(i)+".html";
            self.r.lpush("fruit_spider:start_urls",url)


